package com.choosefine.it.manager.service.Impl;

import java.util.List;
import java.util.Map;

import com.choosefine.it.basedata.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.company.dao.CompanyPublishprojectMapper;
import com.choosefine.it.company.model.CompanyPublishproject;
import com.choosefine.it.company.model.vo.CompanyPublishProjectDetailVO;
import com.choosefine.it.company.model.vo.CompanyPublishProjectVO;
import com.choosefine.it.manager.dao.ManagerProjectCollectionMapper;
import com.choosefine.it.manager.model.ManagerProjectCollection;
import com.choosefine.it.manager.service.ManagerSelectProjectInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @author chenjian
 * @date 2016-11-30 下午5:13:50
 */
@Service("managerSelectProjectInfoService")
public class ManagerSelectProjectInfoServiceImpl implements ManagerSelectProjectInfoService {
	@Autowired
	private CompanyPublishprojectMapper companyPublishprojectMapper;
	@Autowired
	private ManagerProjectCollectionMapper managerProjectCollectionMapper;
	@Autowired
	MongoTemplate mongoTemplate;
	@Autowired
	CountService countService;
/*项目经理找项目列表3.11yuxin*/
	@Override
	public Page<CompanyPublishProjectVO> selectProjectList(Map<String, Object> params, PageDTO pageDto) {
		PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
		Page<CompanyPublishProjectVO> page = companyPublishprojectMapper.selectProjectList(params);
		return page;
	}

	@Override
	public CompanyPublishproject findProjectById(Integer publishId) {
		return companyPublishprojectMapper.findProjectById(publishId);
	}

	@Override
	@Transactional
	public int saveManagerProjectCollection(ManagerProjectCollection managerProjectCollection) {
		return managerProjectCollectionMapper.saveManagerProjectCollection(managerProjectCollection);
	}

	@Override
	@Transactional
	public int cancelManagerProjectCollection(ManagerProjectCollection managerProjectCollection) {
		return managerProjectCollectionMapper.cancelManagerProjectCollection(managerProjectCollection);
	}

	@Override
	public List<ManagerProjectCollection> findeCollectionByCodeAndId(
			ManagerProjectCollection managerProjectCollection) {
		return managerProjectCollectionMapper.findeCollectionByCodeAndId(managerProjectCollection);
	}

	@Override
	@Transactional
	public int repeatManagerProjectCollection(ManagerProjectCollection managerProjectCollection) {
		return managerProjectCollectionMapper.repeatManagerProjectCollection(managerProjectCollection);
	}
/*项目经理查询收藏的项目列表yuxin3.11*/
	@Override
	public Page<CompanyPublishProjectVO> findCollectProjectList(PageDTO pageDTO, Map<String, Object> params) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<CompanyPublishProjectVO> list = managerProjectCollectionMapper.findCollectProjectList(params);
		return list;
	}
/*项目经理收藏yuxin3.11*/
	@Override
	public int managerCollectionProject(Map<String, Object> params) {
		int num=0;
		int count=countService.isInsert(params);
		if(0==count){
			num=companyPublishprojectMapper.managerCollectionProject(params);
			Update update=new Update();
			int favoriteCount=countService.favoriteCount(params);
			update.set("favoriteCount",favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))),update,CompanyPublishproject.class);
		}
		return num;
	}
/*项目经理取消收藏yuxin3.11*/
	@Override
	public int cancelCollectionProject(Map<String, Object> params) {
		int num=0;
		int count=countService.isInsert(params);
		if(count>0){
			num=companyPublishprojectMapper.cancelCollectionProject(params);
			Update update=new Update();
			int favoriteCount=countService.favoriteCount(params);
			update.set("favoriteCount",favoriteCount);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("publishId"))),update,CompanyPublishproject.class);
		}
		return num;
	}
}
